# -*- coding:utf-8 -*-
from django.contrib import admin
from django.utils.safestring import mark_safe
from callcentre.models import TMobileTyping


class TMobileTypingAdmin(admin.ModelAdmin):
    list_display = ('mobile', 'note', 'name', 'sex', 'user_name', 'status', 'mtime',)
    fields = ('mobile', 'name', 'sex', 'user_name', 'note', )
    search_fields = ('mobile', 'name', 'note', )
    list_filter = ('sex', 'status',)
    readonly_fields = ('status',)
    actions = None  # 禁止显示
    ordering = ('-pk',)

    def get_queryset(self, request):
        qs = super(TMobileTypingAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(create_user=request.user.username)

    def get_search_fields(self, request):
        search_fields = ('mobile', 'name', 'note',)
        return search_fields

    def save_model(self, request, obj, form, change):
        def get_ip():
            """获取客户端的IP地址"""
            if 'HTTP_X_FORWARDED_FOR' in request.META:
                return request.META['HTTP_X_FORWARDED_FOR']
            return request.META['REMOTE_ADDR']

        if not change:  # 新增数据时
            user_obj = request.user
            obj.create_user = user_obj.username
            obj.client_ip = get_ip()
            obj.category_name = ''
            obj.channel_name = ''

        obj.save()

    def delete_model(self, request, obj):
        """禁止删除操作"""
        pass

    # def change_view(self, request, object_id, form_url='', extra_context=None):
    #     pass


admin.site.register(model_or_iterable=TMobileTyping, admin_class=TMobileTypingAdmin)

admin.site.site_title = 'MW System'
admin.site.site_header = '管理运维'
admin.site.index_title = '系统模块'
